//
//  StudentDetailViewController.m
//  UI11_Address
//
//  Created by dllo on 15/10/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "StudentDetailViewController.h"
#import "Singleton.h"
#import "ChineseToPinyin.h"
@interface StudentDetailViewController ()<StudentDetailViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, retain) StudentDetailView *studentDV;


@end

@implementation StudentDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.studentDV = [[StudentDetailView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.studentDV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.studentDV];
    [_studentDV release];
    
    self.studentDV.studentDVDelegate = self;
    
    
//接收传过来的值
    self.navigationItem.title = self.dvStudent.name;
    self.studentDV.nameField.text = self.dvStudent.name;
    self.studentDV.phoneField.text = self.dvStudent.phoneNumber;
    self.studentDV.genderField.text = self.dvStudent.gender;
    self.studentDV.headImageView.image = self.dvStudent.myimage;


    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    
}

-(void)presentToViewController{
    
//创建相册
    UIImagePickerController *pic = [[UIImagePickerController alloc]init];
    pic.allowsEditing =YES;
    pic.delegate = self;
    [self presentViewController:pic animated:YES completion:^{
        
    }];

}

//在相册中选头像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSLog(@"info == %@",info);
    self.studentDV.headImageView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
}


-(void)rightButtonAction:(UIBarButtonItem *)button
{
    //找到该学生信息
    NSString *key = [[Singleton shareSingleton].allKeysArray objectAtIndex:self.dvIndexPath.section];
    NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:key];
    
    //根据row删除学生对象
    [array removeObjectAtIndex:self.dvIndexPath.row];
    
    //删除整个section
    if (array.count == 0) {
        
        //删除key对应的数组
        [[Singleton shareSingleton].studentDic removeObjectForKey:key];
        [[Singleton shareSingleton].allKeysArray removeObjectAtIndex:self.dvIndexPath.section];
    }
    
        //新建个学生对象
        Students *dvStu = [[Students alloc]init];
        
        //把修改的值传给这个学生对象
        dvStu.name = self.studentDV.nameField.text;
        dvStu.phoneNumber = self.studentDV.phoneField.text;
        dvStu.gender= self.studentDV.genderField.text;
        dvStu.myimage = self.studentDV.headImageView.image;
       
        //取出新学生姓名的首字母
        char s = [ChineseToPinyin sortSectionTitle:dvStu.name];
        NSString *newKey = [NSString stringWithFormat:@"%c",s];
    
         //如果单例的key数组中包含 新学生姓名的首字母
        if ([[Singleton shareSingleton].allKeysArray containsObject:newKey]) {
            
            //在单例的字典中找到 新学生姓名的首字母 的数组
            NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:newKey];
            
            //把学生加入到这个数组中
            [array addObject:dvStu];
           
        }else{
            //新创建个数组
            NSMutableArray *array = [NSMutableArray array];
            
            //在单例的字典中 加入以新学生姓名的首字母为key 的数组
            [[Singleton shareSingleton].studentDic setObject:array forKey:newKey];
            
            //将新学生加入到数组中
            [array addObject:dvStu];
            
            //将新key加入到 单例key数组中
            [[Singleton shareSingleton].allKeysArray addObject:newKey];
            
            //排序
            [[Singleton shareSingleton].allKeysArray sortUsingSelector:@selector(compare:)];
            }
    [self.sdVCDelegate presentToController];
    [self.navigationController popToRootViewControllerAnimated:YES];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
